🕹 Parallax Controller
Core classes and controller for creating parallax scrolling effects.
This package was created for react-scroll-parallax, but can be used as a standalone lib.
Usage
Create the parallax controller first:
const controller = ParallaxController.init();
Then create an element with animation effects as props:
controller.createElement({
elOuter: document.getElementById('outer'),
elInner: document.getElementById('inner'),
props: {
translateY: [-100, 100],
opacity: [0.4, 1],
},
});
<div id="outer">
<div id="inner">
</div>
</div>
Optimizations to Reduce Jank
A number of techniques are used to keep scrolling optimized:
- Uses a single passive scroll listener to control all animated elements on the page.
- A minimal amount of work is done on the scroll event to prevent jank with no calls to methods that cause layout shifts.
- Calculations that cause layout, reflow (
getBoundingClientRect()
) are cached and only updated when layout may change. - The
requestAnimationFrame
is used to apply all scroll effects. - Only GPU supported CSS effects
transform
and opacity
are allowed. - CSS
will-change
is added to an element based on animation effects to prevent paints.
If you have ideas to further optimize scrolling please PR or post an issue.
⚠️ Scroll Effects May Still Cause Jank and Bad User Experiences!
It's up to you to make sure you use this package appropriately. Here's some suggestions for usage while maintaining good UX:
- Keep effects simple -- less is more. Oftentimes the less extreme animations on the page the better the scrolling will be.
- Minimize the number of scroll effects on elements that are in view at the same time.
- When using images keep them small and optimized. Hi-resolution images will hurt scroll performance.
- Disable most (or all) scroll effects on mobile. Mobile devices optimize for best battery life and animation performance will often be degraded.